package com.opennews.openplatform.jooq;

import lombok.RequiredArgsConstructor;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.TableLike;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class DSLContextWrapper {
    private final DSLContext dslContext;

    public int count(TableLike<?> table, Condition condition) {
        var query = dslContext.selectCount()
                .from(table);

        if (condition != null) {
            query.where(condition);
        }

        Integer count = query.fetchOne(0, Integer.class);

        return count != null ? count : 0;
    }

    public int count(TableLike<?> table) {
        return count(table, null);
    }
}
